package ca.camosun.morsecode;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Gravity;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;


import ca.camosun.morsecode.MorseVibrator;
import ca.camosun.morsecode.librarycode.DeviceListActivity;


public class MorseButtonPadActivity extends Activity implements OnClickListener {
	MorseVibrator mVibrator;

	Button dotButton;
	Button dashButton;
	Button shortPauseButton;
	Button longPauseButton;
	EditText readableMessage;
	private Toast toast;
	DeviceListActivity deviceChecker=new DeviceListActivity();
	MorseQueue toDecode = new MorseQueue();
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_morse_button_pad);

		mVibrator = new MorseVibrator(this.getApplicationContext());
		dotButton = (Button) findViewById(R.id.dot);
		dashButton = (Button) findViewById(R.id.dash);
		shortPauseButton = (Button) findViewById(R.id.short_pause);
		longPauseButton = (Button) findViewById(R.id.long_pause);
		readableMessage = (EditText) findViewById(R.id.displayMorseMessage);
		Intent textIntent = getIntent();
		readableMessage.setText(textIntent.getCharSequenceExtra("text"));
		readableMessage.setSelection(readableMessage.getText().length());

		dotButton.setOnClickListener(MorseButtonPadActivity.this);
		dashButton.setOnClickListener(MorseButtonPadActivity.this);
		shortPauseButton.setOnClickListener(MorseButtonPadActivity.this);
		longPauseButton.setOnClickListener(MorseButtonPadActivity.this);
		
		toast = Toast.makeText(MorseButtonPadActivity.this, "", Toast.LENGTH_SHORT);
		toast.setGravity(Gravity.BOTTOM|Gravity.CENTER, 0, 0);
		// prevent the check from being called whem the device orientation changes 
		// start the bluetooth connectivity activity
		Intent bluetoothIntent=new Intent(this,DeviceListActivity.class); 

		startActivity(bluetoothIntent);
		
		}
		
		// don't finish this activity, because we don't want to have to start it again 
	

	@Override
	public void onClick(View applicationView) {
		Button buttonToAnimate;
		Animation typeWriter = AnimationUtils.loadAnimation(this, R.layout.button_animation);
		
		switch (applicationView.getId()) {
		case R.id.dot:
			buttonToAnimate = (Button) findViewById(R.id.dot);
			buttonToAnimate.startAnimation(typeWriter);
			
			mVibrator.morseVibrate(MorseDecoder.Token.DOT);
			toDecode.addToken(MorseDecoder.Token.DOT);
			showToast(".");
			break;
		case R.id.dash:
			buttonToAnimate = (Button) findViewById(R.id.dash);
			buttonToAnimate.startAnimation(typeWriter);
			
			mVibrator.morseVibrate(MorseDecoder.Token.DASH);
			toDecode.addToken(MorseDecoder.Token.DASH);
			showToast("-");
			break;
		case R.id.short_pause:
			buttonToAnimate = (Button) findViewById(R.id.short_pause);
			buttonToAnimate.startAnimation(typeWriter);
			
			mVibrator.morseVibrate(MorseDecoder.Token.SHORT_PAUSE);
			toDecode.addToken(MorseDecoder.Token.SHORT_PAUSE);
			showToast("short pause");
			outputMessage();
			break;
		case R.id.long_pause:
			buttonToAnimate = (Button) findViewById(R.id.long_pause);
			buttonToAnimate.startAnimation(typeWriter);
			
			mVibrator.morseVibrate(MorseDecoder.Token.LONG_PAUSE);
			toDecode.addToken(MorseDecoder.Token.LONG_PAUSE);
			showToast("long pause");
			outputMessage();
			break;
		default:
			break;
		}
	}

	void outputMessage() {
		String decoded = "";
		
		while (toDecode.hasNext()) {
			decoded += toDecode.getNextDecoded();
		}
		
		if(decoded.contains(MorseDecoder.BAD_MORSE)) {
			decoded = decoded.replace(MorseDecoder.BAD_MORSE, "");
			showToast("invalid character");
		}
		
		readableMessage.append(decoded);
		readableMessage.setSelection(readableMessage.getText().length());
	}
	
	void showToast(String text) {
		toast.setText(text);
		toast.show();
	}
	
    @Override
    public boolean onCreateOptionsMenu(Menu menu)
    {
        MenuInflater menuInflater = getMenuInflater();
        menuInflater.inflate(R.menu.activity_morse_button_pad, menu);
        return true;
    }
    
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        Intent inputModeIntent = null;
        
        switch (item.getItemId()) {
           
        case R.id.manual_morse:
        	inputModeIntent = new Intent(this, ManualMorseActivity.class);
        	inputModeIntent.putExtra("text", readableMessage.getText());
        	finish();
        	startActivity(inputModeIntent);
            return true;
        }
        return false;
    }

    
    public String toString() {

    	return readableMessage.getText().toString();

    }
    
}
